<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Traits;

use Hyperf\Contract\LengthAwarePaginatorInterface;

/**
 * Trait ServiceTrait.
 */
trait ServiceTrait
{
    /**
     * 查询列表.
     */
    public function getList(?array $params = null): mixed
    {
        // 存在字段查询
        if ($params['select'] ?? null) {
            $params['select'] = explode(',', $params['select']);
        }
        return $this->dao->getList($params);
    }

    /**
     * 带分页查询.
     */
    public function getPageList(?array $params = null): mixed
    {
        // 存在字段查询
        if ($params['select'] ?? null) {
            $params['select'] = explode(',', $params['select']);
        }
        return $this->dao->getPageList($params);
    }

    /**
     * 新增数据.
     */
    public function save(array $data): int
    {
        return $this->dao->save($data);
    }

    /**
     * 更新一条数据.
     */
    public function update(int $id, array $data): bool
    {
        return $this->dao->update($id, $data);
    }

    /**
     * 单个或批量删除数据.
     */
    public function delete(array $ids): bool
    {
        return !empty($ids) && $this->dao->delete($ids);
    }

    /**
     * 设置数据库分页.
     */
    public function setPaginate(LengthAwarePaginatorInterface $paginate): array
    {
        return [
            'items' => $paginate->items(),
            'pageInfo' => [
                'total' => $paginate->total(),
                'currentPage' => $paginate->currentPage(),
                'totalPage' => $paginate->lastPage(),
            ],
        ];
    }
}
